package ipaintplugin.objects;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Rectangle extends Polygon {

	public static final String TAG_NAME = "rectangle";
	private Point min, max;

	public Rectangle() {

	}

	@Override
	public void fromXML(Node el) {
		min = new Point(Integer.parseInt(el.getAttributes()
				.getNamedItem("minx").getNodeValue()), Integer.parseInt(el
				.getAttributes().getNamedItem("miny").getNodeValue()));

		max = new Point(Integer.parseInt(el.getAttributes()
				.getNamedItem("maxx").getNodeValue()), Integer.parseInt(el
				.getAttributes().getNamedItem("maxy").getNodeValue()));
		
		super.fromXML(el);
	}

	public Point getMin() {
		return min;
	}

	public void setMin(Point min) {
		this.min = min;
	}

	public Point getMax() {
		return max;
	}

	public void setMax(Point max) {
		this.max = max;
	}

	@Override
	public void draw(GC canvas) {
		super.draw(canvas);

		if (fillColor != null && fillColor != getColor(SWT.TRANSPARENT))
			canvas.fillRectangle(min.getX(), min.getY(), max.getX()
					- min.getX(), max.getY() - min.getY());
		
		canvas.drawRectangle(min.getX(), min.getY(), max.getX()
				- min.getX(), max.getY() - min.getY());
		
		
	}

	@Override
	public void toXML(Document doc, Element root) {

		Element el = doc.createElement(TAG_NAME);
		super.toXML(doc, el);

		el.setAttribute("minx", String.valueOf(min.getX()));
		el.setAttribute("miny", String.valueOf(min.getY()));
		el.setAttribute("maxx", String.valueOf(max.getX()));
		el.setAttribute("maxy", String.valueOf(max.getY()));

		root.appendChild(el);
	}

	@Override
	public String getTagName() {
		return TAG_NAME;
	}

	@Override
	public Object clone() {
		// TODO Auto-generated method stub
		return new Rectangle();
	}

}
